package com.polaris.live.popup.party

import android.content.Context
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.PopupSetEntryConditionsBinding
import com.polaris.live.net.liveService
import com.polaris.live.resp.go_resp.JoinConditionRequest
import com.polaris.live.translation.R.string
import com.polaris.live.viewmodel.party.PartyAnchorFragmentModel

/**
 *  设置入驻条件
 * @author Created by 天晴 on 2024/10/15 19:18
 * @since 1.0.0
 **/
class SetEntryConditionsPopup(
    context: Context,
    val mViewModel: BaseViewModel,
    val roomId: String,
) : BottomPopupView(context) {
    private lateinit var mBinding: PopupSetEntryConditionsBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_set_entry_conditions
    }

    private var condition = NO

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupSetEntryConditionsBinding.bind(popupImplView)
        getJoinCondition(roomId)
        mBinding.apply {
            cancel.setOnClickListener {
                dismiss()
            }
            btnClose.setOnClickListener {
                dismiss()
            }
            confirm.setOnSingleClickListener {
                updateConditions(condition)
            }
            noLl.setOnClickListener {
                condition = NO
                updateCheck()
            }
            membersLl.setOnClickListener {
                condition = MEMBERS
                updateCheck()
            }
        }
    }

    private fun updateCheck() {
        mBinding.apply {
            when (condition) {
                NO -> {
                    noCk.setBackgroundResource(R.drawable.selector_conditions_checked)
                    membersCk.setBackgroundResource(R.drawable.selector_conditions_unchecked)
                }

                MEMBERS -> {
                    noCk.setBackgroundResource(R.drawable.selector_conditions_unchecked)
                    membersCk.setBackgroundResource(R.drawable.selector_conditions_checked)
                }
            }
        }

    }

    /**
     * 更新条件
     * 价格 0无 1房间成员
     */
    private fun getJoinCondition(roomId: String) {
        mViewModel.requestLoading({
            liveService.getJoinCondition(roomId)
        }, {
            condition = if (it.type == 1) MEMBERS else NO
            updateCheck()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })

    }

    /**
     * 更新条件
     * 价格 0无 1房间成员
     */
    private fun updateConditions(condition: Long) {
        mViewModel.requestLoading({
            liveService.joinCondition(JoinConditionRequest(condition))
        }, {
            ToastGravity.showGravity(string.modified_successfully)
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })

    }

    companion object {
        const val NO = 0L
        const val MEMBERS = 1L

        fun show(
            context: Context,
            mViewModel: PartyAnchorFragmentModel,
            roomId: String,
        ) {
            XPopup.Builder(context)
                .isViewMode(false)
                .hasNavigationBar(false)
                .navigationBarColor(R.color.color_6448ff)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(SetEntryConditionsPopup(context, mViewModel, roomId))
                .show()
        }
    }


}